package d20chat;

import java.net.Socket;
import java.util.Scanner;


/**
 * Stores information about a single user.
 */
public class User
{
    /** An instance of the Profile class to hold the profile info for the user. */
    public Profile userProfile; 
    /**
     * a single word tag for others to refer to the user by.
     * (such as: BlueSun) (no spaces allowed)
     * must be unique for all users
     */
    private String userName = "";
    /**
     * any name (can include spaces) the user wants to be known as for game.
     * default to player if they don't want a more specific title
     */
    private String characterName = "player"; 
    /**
     * the connection information the Server keeps for user.
     */
    private Socket connection;  
    /**
     * is user not allowed to talk, allow user to talk unless set otherwise.
     */
    private boolean silenced = false; 
    

    
    /**
     * Turns username plus profile into a string with elements seperated by \u8594
     * @return returns a \u8594 seperated String representing username and profile info
     */
    public String toString()
    {
        String theString = "";
        theString = "\u8594" + userName + "\u8594" + userProfile.getProfile();
        return theString;
    }
    
    
    /**
     * Takes a String representing user info and stores it.
     * @param info A String seperated by \u8594a, made up of word + a string built by User.toString();
     * @return A String holding the username of the person who's info it was.
     */
    public String setUserInfo(String info)
    {
        //The varaibles that make up a profile
        String tname = ""; //name
        String temail = ""; //email
        String taboutMe = ""; //aboutMe
        String twebsite = ""; //website
        String tphone = ""; //phone number
        boolean tvisible = true; //whether the profile is shared or not
        
        //create a scanner object to find the pieces
        //unicode character '\u8594' seperates each peice
        //could not use spaces because many peices are multi word
        Scanner pieceScanner = new Scanner( info );
        pieceScanner.useDelimiter( "\u8594" );
      
        //there is a word we don't need to start any message like this
        if( pieceScanner.hasNext() )
        {
            //junk piece
          pieceScanner.next();
        }
        
        //get the username
        if( pieceScanner.hasNext() )
        {
          userName = pieceScanner.next(); 
        }
       
        //get the profile pieces and hold them
        if( pieceScanner.hasNext() )
        {
          tname = pieceScanner.next();
        }
        
        if( pieceScanner.hasNext() )
        {
          temail = pieceScanner.next();
        }
        
        if( pieceScanner.hasNext() )
        {
          taboutMe = pieceScanner.next();
        }
        
        if( pieceScanner.hasNext() )
        {
          twebsite = pieceScanner.next();
        }
        
        if( pieceScanner.hasNext() )
        {
          tphone = pieceScanner.next();
        }
        
        if( pieceScanner.hasNext() )
        {
            //this is recieved as a String, but profile expects boolean
            //so convert it
          tvisible = Boolean.parseBoolean(pieceScanner.next());
        }
        
        //call set profile with the information gathered
        //setProfile(String tname, String temail, String taboutMe, String twebsite, String tphone, boolean tvisible)
        userProfile.setProfile(tname, temail, taboutMe, twebsite, tphone, tvisible);
        
        return userName;
    }
    
    
    /**
     * Sets username removing spaces, <, >, [, ], (, ) and cutting it off after 25 characters.
     * @param tname the new username to use
     */
    public void setUsername(String tname)
    {

        if( tname.length() > 25 )
        {
            tname = tname.substring( 0, 25 );
        }

        tname = tname.replaceAll("\\<","");
        tname = tname.replaceAll("\\>","");
        tname = tname.replaceAll("\\[","");
        tname = tname.replaceAll("\\]","");
        tname = tname.replaceAll("\\)","");
        tname = tname.replaceAll("\\(","");
        tname = tname.replaceAll("\\s","");
        userName = tname;

    }
    
    
    /**
     * Sets the user's charactername to cname.
     * Currently no limits on cnam, that may need to be changed in the future.
     * @param cname a game related name for the user, can have spaces
     */
    public void setCharacterName(String cname)
    {
        characterName = cname;
    }
    
    

    /**
     * Sets the user's connection information for the server.
     * @param setSock the user's socket
     */
    public void setSocket(Socket setSock)
    {
        connection = setSock;
    }
    

    

    /**
     * Sets the user to silenced or not.
     * @param tsilence user's silence state
     */
    public void setSilenced(boolean tsilence)
    {
        silenced = tsilence;
    }

    

    /**
     * Returns the user's username.
     * @return String holding username
     */
    public String getUsername()
    {
        return userName;
    }
    
    
    /**
     * Returns the user's charactername.
     * @return String holding the character name for the user.
     */
    public String getCharacterName()
    {
        return characterName;
    }
    
    

    /**
     * Returns the user's connection information.
     * @return A Socket object holding hte user's connection information.
     */
    public Socket getSock()
    {
        return connection;
    }
    
    

    /**
     * Returns whether user is silenced.
     * @return Boolean holding true if user is sileneced, false if not.
     */
    public boolean getSilenced()
    {
        return silenced;
    }
    
    
    /**
     * Constructor - creates the profile that is part of User.
     */
    public User()
    {
        //instanciate the profile
        userProfile = new Profile();
    }
    
}
